/*
* $Id: ComReader.h 347 2012-02-29 19:58:23Z volok-aleksej@yandex.ru $
*
* Copyright 2009-2011 by Volokitin Aleksey
* All rights reserved
*
*/
#ifndef _COM_READER_H_
#define _COM_READER_H_

#include "Thread.h"
#include "Database/InterfaceDb.h"
#include "Database/TransactManager.h"

class CComError : public CBaseError
{
public:
	CComError(const std::tstring& strFileName, const std::tstring& strFunction, int nLine, const std::tstring& msg);
};

class CComReader : public CThread, public IReader
{
	std::tstring m_strComFileName;
	bool m_bWrite;
	HANDLE m_hFile;
	IDatabase* m_database;
	std::map<int, IServOperation*> m_aOperations;
	std::map<int, std::vector<int> > m_aFamilies;
	time_t m_begin;
	time_t m_end;
	CTransactManager m_manager;
	INotifier* m_pNotifier;
public:
	CComReader(INotifier* pNotifier, bool bWrite);
	virtual ~CComReader();
public:
	void SetComFileName(const std::tstring& strFileName);
public:
	virtual int ReadUTF(std::tstring& value);
	virtual int ReadInt(__int32& value);
	virtual int ReadBool(bool& value);
	virtual int ReadTime(__int64& value);
	virtual void WriteUTF(const std::tstring& value);
	virtual void WriteInt(__int32 value);
	virtual void WriteBool(bool value);
	virtual void WriteTime(__int64 value);

	void SetWriteFamilies(const std::map<int, std::vector<int> >& aFamilies);
	void SetInterval(time_t begin, time_t end);

	void Read(char* data, int len);
	void Write(char* data, int len);
protected:
	virtual int ThreadFunc();
	virtual void Stop();
	virtual void Resume();
	virtual void Pause();
private:
	void GetWidcommComPath();
	void GetBTComPath();
	void ReadData();
	void WriteDBData(int id, ITable::i_content* pContent);
	void WriteDBData(int id, ITable* pTable);
};

#endif/*_COM_READER_H_*/